
/** 初始化设置等都在 summary-req.js 中**/
var pageX = {
        actions : [],
        hasInitEdit : false,
        wfState : "view",
        showToast : true,//只拉一次横幅
        wfDesigner : null,
        cache : {},
        popCount : 0,//弹层次数
        backLock : false
}
pageX.winParams = {
        "canEditWorkflow" : true,
        "cache_subfix" : "",//缓存后缀
        "WebviewEvent" : "",//多webview事件
        "edit" : false,//初始化状态
        "action" : "",//WorkFlowLock.SUBMIT,// 提交操作
        "canBack" : true//是否可执行返回操作， === false表示不能执行返回
     }
     pageX.winParams = cmp.extend(true, pageX.winParams, cmp.href.getParam());
window.summaryBO = pageX.winParams.summaryBO;
cmp.ready(function(){

    _initBackEvent();
	cmp.dialog.loading();
	
    cmp.i18n.init(_collPath + "/i18n/", "Collaboration", function(){
        _renderMainPage();
    });
});


/**
 * 首屏数据加载完成后，渲染主入口
 */
function _renderMainPage(){
    
    LazyUtil.addLazyStack({
        "code" : "lazy_cmp",
        "css" : [
                _cmpPath + "/css/cmp-selectOrg.css" + $verstion,
                _cmpPath + "/css/cmp-picker.css" + $verstion,
                _cmpPath + "/css/cmp-search.css" + $verstion,
                _cmpPath + "/css/cmp-accDoc.css" + $verstion,
                _cmpPath + "/css/cmp-audio.css" + $verstion
                 ],
        "js" : [
                _cmpPath + "/js/cmp-flowV5.js" + $verstion,
                _cmpPath + "/js/cmp-accDoc.js" + $verstion,
                _cmpPath + "/js/cmp-push.js" + $verstion,
                _cmpPath + "/js/cmp-audio.js" + $verstion,
                _cmpPath + "/js/cmp-server.js" + $verstion,
                _cmpPath + "/js/cmp-lbs.js" + $verstion,
                _cmpPath + "/js/cmp-selectOrg.js" + $verstion,
                _cmpPath + "/js/cmp-picker.js" + $verstion,
                _cmpPath + "/js/cmp-dtPicker.js" + $verstion,
                _cmpPath + "/js/cmp-search.js" + $verstion,
                _cmpPath + "/js/cmp-popPicker.js" + $verstion,
                _cmpPath + "/js/cmp-camera.js" + $verstion,
                _cmpPath + "/js/cmp-webviewListener.js" + $verstion,
                _cmpPath + "/js/cmp-v5.js" + $verstion,
                _cmpPath + "/js/cmp-barcode.js" + $verstion
                ]
    });
    
    LazyUtil.addLazyStack({
        "code" : "lazy_thispage",
        "css" : [],
        "js" : [
                _collPath + "/collaboration_m_api.s3js" + $verstion
            ]
    });
    
  //意见相关
    LazyUtil.addLazyStack({
        "code" : "lazy_listView",
        "css" : [
                 _cmpPath + "/css/cmp-listView.css" + $verstion,
                 _cmpPath + "/css/cmp-att.css" + $verstion
                 ],
        "js" : [
                _cmpPath + "/js/cmp-imgCache.js" + $verstion,
                _cmpPath + "/js/cmp-listView.js" + $verstion,
                _cmpPath + "/js/cmp-att.js" + $verstion,
                _collPath + "/js/mplus_apps_collaboration.s3js" + $verstion,
                _cmpPath + "/js/cmp-emoji.js" + $verstion
                ]
    });
    
  //添加懒加载数组
    LazyUtil.addLazyStack({
        "code" : "lazy_wf",
        "depend" : "lazy_listView",
        "dependModel" : "strong",
        "css" : [_wfPath + "/css/wf.css" + $verstion],
        "js" : [_cmpPath + "/js/cmp-zoom.js" + $verstion]
    });

        LazyUtil.addLazyStack({
            "code" : "lazy_sliders",
            "depend" : "lazy_listView",
            "css" : [
                     _cmpPath + "/css/cmp-sliders.css" + $verstion
                     ],
            "js" : [
                    _cmpPath + "/js/cmp-sliders.js" + $verstion
                    ]
        });
    
    LazyUtil.addLazyStack({
        "code" : "lazy_edit",
        "js" : [_cmpPath + "/js/cmp-contentEdit.js" + $verstion,
                _common_v5_path + "/widget/ContentEdit-debug.js" + $verstion
                ],
    });
    
    _fillPage();
    
}


//装载页面
function _fillPage() {
    
    
  //初始化正文区域
    _initLayout(false);
    _toggleContent(false);
    
    //尝试加载正文
    _initContent();
}


//存储状态数据
function _storagePageData(){
    
    if(pageX.viewScroll && pageX.viewsLenth > pageX.viewMax){
        pageX.cache.scollLeft = pageX.viewScroll.x;
    }
    
	cmp.storage.save(_storge_key, cmp.toJSON(pageX.cache), true);
}


//设置元素布高度等
function _initLayout(isRefresh, tHeight, setTitle){
    
  //设置正文区域高度
        var bodyHeight = 0,
        titleHeight = 0;

    //优化渲染数度
     var headerDom = _$("#hid");
    var hStyleHeight = headerDom.style.height;
    if(hStyleHeight){
        headerH = parseInt(hStyleHeight, 10);
    }else{
        //offsetHeight 优化渲染
        headerH =  headerDom.offsetHeight;
        headerDom.style.height = headerH + "px";
    }
        
    var $bodyContent = _$("#body_content_div");
    var bStyleHeight = $bodyContent.style.height;
    if(bStyleHeight){
        bodyHeight = parseInt(bStyleHeight, 10);
    }else{
        //offsetHeight 优化渲染
        bodyHeight =  $bodyContent.offsetHeight;
        $bodyContent.style.height = bodyHeight + "px";
    }
    
    if(typeof tHeight != "undefined" && tHeight != null){
        titleHeight = tHeight;
    }else{
        var tStyleHeight = 0;
        if(tStyleHeight){
            titleHeight = parseInt(tStyleHeight, 10);
        }else{
            //offsetHeight 优化渲染
            titleHeight =  0;
        }
    }
    
    var fHeight = 0;
    
    var cHeight = bodyHeight - titleHeight;
    _$("#summary_info").style.height = cHeight + "px";
    pageX.contentHeight = cHeight;
}


//附件区域显示和影藏
function _toggleContent(show){return;
}

//绑定事件
function _bindEvent(){
    
    //处理人列表
    _$("#allAffairBtn").addEventListener("tap", function(){
        _initHandlerList("all");
    });
    _$("#runnintAffariBtn").addEventListener("tap", function(){
        _initHandlerList("running");
    });
    
    //工作流展现
    _$("#workflowBtn").addEventListener("tap", _showWorkflow);
    
  //正文展现
    _$("#_contentBtn").addEventListener("tap", _showConent1);
    
    //此处监听sui_form_customcontrol_render事件，当表单中自定义控件发生渲染时，会触发此事件
    document.addEventListener('sui_form_customcontrol_render', function(e){
    	_customcontrol_meetingroom_init(e);
    });
}



//返回上一个页面
function _goBack(){
    
	if(pageX.backLock){
        return;
    }
    pageX.backLock = true;
    
    var $editBtn = _$("#editBtn");
    //if((backNoCheck===true) || !$editBtn || pageX.wfState == "view"){
    if((backNoCheck===true) || !$editBtn || pageX.wfState == "view" || backNoCheck === "btnToBack"){
        
        var backEventName;
        if(backNoCheck !== true){
            
            backEventName = WebEvents.M3_EVENT_SUMMARY + pageX.winParams.cache_subfix
            summaryBO.backIndex = summaryBO.backIndex - 1;
            cmp.storage.save(CollCacheKey.summary.summaryBO + pageX.winParams.cache_subfix, cmp.toJSON(window.summaryBO), true);
        }else{
           //点完成执行返回
        }
        cmp.href.back(1, null, backEventName);
    }else{
        //这个分支暂时不走了
        //不能返回了，这个是用来屏蔽android返回按钮的
    }
}


function _initBackEvent(){
	cmp.backbutton();
    cmp.backbutton.push(_exeBack);
    _$("#goBackBtn").addEventListener("tap", _exeBack);
}

function _exeBack(){
    
	if(pageX.wfState == "edit"){
        cmp.event.trigger("beforepageredirect", document);
    }
    
    if(pageX.winParams.edit && pageX.winParams.canBack !== false){
        //处理页面入口进入
        _goBack(true);
    }else{
        //普通页面
        _goBack("btnToBack");
    }
}

//返回
function _goBack(backNoCheck) {

    if(pageX.backLock){
        return;
    }
    pageX.backLock = true;
    
    var $editBtn = _$("#editBtn");
    //if((backNoCheck===true) || !$editBtn || pageX.wfState == "view"){
    if((backNoCheck===true) || !$editBtn || pageX.wfState == "view" || backNoCheck === "btnToBack"){
        
        var backEventName;
        if(backNoCheck !== true){
            
            backEventName = WebEvents.M3_EVENT_SUMMARY + pageX.winParams.cache_subfix
            summaryBO.backIndex = summaryBO.backIndex - 1;
            cmp.storage.save(CollCacheKey.summary.summaryBO + pageX.winParams.cache_subfix, cmp.toJSON(window.summaryBO), true);
        }else{
           //点完成执行返回
        }
        cmp.href.back(1, null, backEventName);
    }else{
        //这个分支暂时不走了
        //不能返回了，这个是用来屏蔽android返回按钮的
    }
}

//注册成全局的，原表单也会调用
function onContentScollButton(){
  
    //正文未加载后才执行
    if(_isContentInit){
        if(pageX.toggleLableCount != 0){
            _toggleContent(true);//展示意见区域
        }
    }
}

//注册成全局的，原表单也会调用
function onContentScollTop(top, directionY){
 
    //正文未加载后才执行
    if(_isContentInit){
        
        if(!arguments || arguments.length == 0){
            _toggleTitle(true);
        }else{
            //-1下拉， 1上拉
            if(directionY == -1 && top < 10){
                _toggleTitle(true);
            }else if(directionY == 1 && top > 10){
                _toggleTitle(false);
            }
        }
    }
}

//正文加载完成后方法
function _contentInit(){
   
    cmp.dialog.loading(false);
    
    if(window.summaryBO.info_msg){
        _alert(window.summaryBO.info_msg);
        delete window.summaryBO.info_msg;
    }
    
    _isContentInit = true;
    
    if(pageX.cache.focusOffice){
        //正文初始化时，不是在正文页签
        pageX.cache.focusOffice = false;
        _switche2View(pageX.officeIdex, false);
    }
    
  //启动正文后面的问价
    LazyUtil.startLazy();
}

//加载正文
function _initContent(){
    
    //重复加载正文判定
    if(pageX.initContent){
        return;
    }
    pageX.initContent = true;
    
    var bodyType, content;
    
    content = summaryBO.content1;
    if(content != null){
        
        if(typeof pageX.cache.isLightForm != "boolean"){
            pageX.cache.isLightForm = !!content["isLightForm"];
        }
        
        bodyType = content["contentType"];
        
        
        var contentConfig = {
                "target" : "newInputPosition",
                "bodyType" : bodyType,
                "content" : "",
                "moduleType" : "4",//这个地方只能是公文
                "momentum" : true,
                "onload" : _contentInit,
                //"onScrollBottom" : onContentScollButton, 屏蔽这个方法， 不自动拉起意见了
                "onScroll" : onContentScollTop
            }
        
        //正文
        if(bodyType == SeeyonContent.getBodyCode("HTML") && !content.isForwardForm){
            //HTML正文
            contentConfig.content = content["contentHtml"];
            contentConfig.ext = {
                    reference : summaryBO.summary.id
            }
        }else if(bodyType == SeeyonContent.getBodyCode("HTML") && content.isForwardForm){
            
            pageX.cache.isFormForward = true;
            contentConfig.bodyType = "20-Forward";
            contentConfig.content = content["moduleId"];
            contentConfig.ext = {
                    
                    isNew : false,
                    viewState : summaryBO._viewState,
                    "openFrom" : pageX.winParams["openFrom"],
                    templateId : summaryBO.summary.templateId,
                    
                    
                    rightId : content["formRightId"],
                    allowQRScan : false,
                    "indexParam" : "0",
                    "contentDataId" : content["contentDataId"],
                    "affairId" : summaryBO.summary.affairId,
                    "isLightForm" : false,
                }
        }else if(bodyType == SeeyonContent.getBodyCode("FORM")){}else{
            contentConfig.content = content["fileId"];
            contentConfig.lastModified = content["lastModified"]; 
        }

        //初始化正文
        SeeyonContent.init(contentConfig);
    }else{
        cmp.dialog.loading(false);
        _alert(cmp.i18n("collaboration.nodata.content"), _goBack);
    }
}


//刷新页面布局
function fnRefreshPage() {
  //延迟加载意见
    LazyUtil.addLoadedFn("lazy_listView", function(){
        cmp.listView("#contentView").refresh();
    });
}

